/**
 * 
 */
package tp.pr5.console;

import java.util.Scanner;

import tp.pr5.Constants;
import tp.pr5.Controller;
import tp.pr5.Interpreter;
import tp.pr5.RobotEngine;
import tp.pr5.instructions.Instruction;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;

/**
 * @author Victor y Henar
 * The controller employed when the application is configured as a console application. It contains the simulation loop that executes the instructions written by the user on the console.
 */
public class ConsoleController extends Controller{

	
	/**
	 * Constructor of the controller. It receives the model main class.
	 * @param game Game that is being played.
	 */
	public ConsoleController(RobotEngine game) {
		super(game);
	}

	@Override
	public void startController(){
		Scanner sc = new Scanner (System.in);
		this.game.requestStart();

		while (!this.game.isOver())
		{
			Instruction instruction;
			this.game.saySomething(Constants.PROMPT);
			try {
				instruction = Interpreter.generateInstruction(sc.nextLine());
				this.game.communicateRobot(instruction);
				
			} catch (WrongInstructionFormatException e) {
				this.game.requestError(e.getMessage());
			}
		}
		sc.close();
	}

}
